package com.cyy.defstep;

import com.cyy.utils.Common;
import com.cyy.contanst.GlobalVar;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.zh_cn.假如;
import io.cucumber.java.zh_cn.那么;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Map;


// 通用测试步骤
@Slf4j
public class UtilsDefstep extends AbstractDefstep {

    @Autowired
    Common common;

    /**
     * 设置变量
     *
     * @param type  变量的类型
     *              boolean
     *              byte
     *              string
     *              short
     *              int
     *              long
     *              float
     *              double			64		Double
     * @param name  变量的名称
     * @param value 变量值
     */
    @假如("^设置变量:(.*?) (.*?) = (.*?)$")
    public void setVariable(String type, String name, String value) {
        String rplValue = common.replace(value).toString();

        switch (type) {
            case "boolean":
                GlobalVar.value.put(name, Boolean.parseBoolean(rplValue));
                break;
            case "byte":
                GlobalVar.value.put(name, Byte.parseByte(rplValue));
                break;
            case "string":
                GlobalVar.value.put(name, rplValue);
                break;
            case "short":
                GlobalVar.value.put(name, Short.parseShort(rplValue));
                break;
            case "int":
                GlobalVar.value.put(name, Integer.parseInt(rplValue));
                break;
            case "long":
                GlobalVar.value.put(name, Long.parseLong(rplValue));
                break;
            case "float":
                GlobalVar.value.put(name, Float.parseFloat(rplValue));
                break;
            case "decimal":
                GlobalVar.value.put(name, BigDecimal.valueOf(Long.parseLong(rplValue)));
                break;
            default:
                GlobalVar.value.put(name, rplValue);
                break;

        }
    }

    /**
     * 设置变量，变量的类型为map或list类型，设置方式是在测试步骤下编写table对象,如果table行数为1行，则变量类型为map，如果多余1行，则为list类型
     *
     * @param name  变量名
     * @param table table
     */
    @假如("^设置对象变量:(.*?)$")
    public void setVariable(String name, DataTable table) {
        if (table.asMaps().size() == 1) {
            Map<Object, Object> objectObjectMap = table.asMaps(String.class, Object.class).get(0);
            GlobalVar.value.put(name, objectObjectMap);
        } else {
            GlobalVar.value.put(name, table.asMaps(String.class, Object.class));
        }
    }

    @那么("^打印消息:(.*?)$")
    public void print(String msg) {
        Object output = common.replace(msg);
        log.info(output.toString());
    }

    @假如("^设置随机变量:(.*?) >> (\\d+|-\\d+) ~ (\\d+|-\\d+)$")
    public void setVariable(String name, int start, int end) {
        GlobalVar.value.put(name, common.randInt(start, end));
    }

    @假如("^设置时间变量:(.*?) >> (\\d+|-\\d+)(.*?)【(.+)】$")
    public void today_delta(String name, int delta, String field, String dateFormat) throws ParseException {
        switch (field) {
            case "s":
                GlobalVar.value.put(name, common.today_delta(dateFormat, delta, Calendar.SECOND));
                break;
            case "m":
                GlobalVar.value.put(name, common.today_delta(dateFormat, delta, Calendar.MINUTE));
                break;
            case "h":
                GlobalVar.value.put(name, common.today_delta(dateFormat, delta, Calendar.HOUR));
                break;
            case "d":
                GlobalVar.value.put(name, common.today_delta(dateFormat, delta, Calendar.DAY_OF_MONTH));
                break;
            case "M":
                GlobalVar.value.put(name, common.today_delta(dateFormat, delta, Calendar.MONTH));
                break;
            case "q":
                GlobalVar.value.put(name, common.today_delta(dateFormat, 3 * delta, Calendar.MONTH));
                break;
            case "y":
                GlobalVar.value.put(name, common.today_delta(dateFormat, delta, Calendar.YEAR));
                break;
            default:
                GlobalVar.value.put(name, "null");
                break;
        }
    }
}
